package org.quasar.airNova.presentationLayer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;

import mail.SendMailSSL;

import org.quasar.airNova.businessLayer.CalendarDate;
import org.quasar.airNova.businessLayer.Realizacao;
import org.quasar.airNova.businessLayer.Voo;
import org.quasar.airNova.persistenceLayer.Database;

import pdfGenerator.GeneratePdf;

import com.itextpdf.text.DocumentException;

import javax.swing.JButton;
import javax.xml.crypto.Data;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class CancelarVoo extends JFrame {
	private JTextField textField;
	private JTextField textField_4;
	private JLabel lblDia;
	private JLabel lblMs;
	private JLabel lblAno;
	private JTextField textField_5;
	private JTextField textField_6;
	private JButton btnEnviar;
	private JButton btngerarPDF;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					CancelarVoo frame = new CancelarVoo();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public CancelarVoo() {
		Database.open("database", "AirNova", "db4o");
		setTitle("Cancelar Voo");
		setResizable(false);
		setVisible(true);
		setSize(300, 225);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		getContentPane().setLayout(null);

		textField = new JTextField();
		textField.setBounds(153, 22, 86, 20);
		getContentPane().add(textField);
		textField.setColumns(10);

		textField_4 = new JTextField();
		textField_4.setBounds(153, 57, 86, 20);
		getContentPane().add(textField_4);
		textField_4.setColumns(10);

		JLabel lblNomeDoAviao = new JLabel("Nome do Voo");
		lblNomeDoAviao.setBounds(21, 25, 122, 14);
		getContentPane().add(lblNomeDoAviao);

		lblDia = new JLabel("Dia");
		lblDia.setBounds(21, 60, 46, 14);
		getContentPane().add(lblDia);

		lblMs = new JLabel("M\u00EAs");
		lblMs.setBounds(21, 100, 46, 14);
		getContentPane().add(lblMs);

		lblAno = new JLabel("Ano");
		lblAno.setBounds(21, 128, 46, 14);
		getContentPane().add(lblAno);

		textField_5 = new JTextField();
		textField_5.setBounds(153, 97, 86, 20);
		getContentPane().add(textField_5);
		textField_5.setColumns(10);

		textField_6 = new JTextField();
		textField_6.setBounds(153, 125, 86, 20);
		getContentPane().add(textField_6);
		textField_6.setColumns(10);

		JButton btnCancelarVoo = new JButton("Cancelar Voo");
		btnCancelarVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				CalendarDate cd = new CalendarDate(Integer.parseInt(textField_4
						.getText()), Integer.parseInt(textField_5.getText()),
						null, Integer.parseInt(textField_6.getText()));
				Realizacao rea = null;
				System.out.println(Voo.allInstances().size());
				Voo voo = Database.get(Voo.class, "numero", textField.getText());
				for (Realizacao each : voo.realizacao()) {
					if (each.ocorre_em().isEqual(cd)) {
						rea = each;
						rea.setCancelada(true);
						break;

					}
				}
				Database.update(voo);

				System.out.println(rea.toString());
				System.out.println(voo.toString());

				// Database.delete(voo);
				//
				// System.out.println(Voo.allInstances());
				//
				// System.out.println(Voo.allInstances().size());

			}
		});
		btnCancelarVoo.setBounds(10, 162, 95, 23);
		getContentPane().add(btnCancelarVoo);

		btnEnviar = new JButton("Enviar");
		btnEnviar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==btnEnviar){
					String sender = JOptionPane.showInputDialog("Insira o seu mail");
					String password = JOptionPane.showInputDialog("Insira a password");
					String receiver = JOptionPane.showInputDialog("Insira o mail de destino");
					String title = "Voo Cancelado";
					String content = "Voo " + textField.getText()
							+ " Cancelado\n" + "Data prevista para o Voo:"
							+ textField_4.getText() + "/"
							+ textField_5.getText() + "/"
							+ textField_6.getText();
					
					SendMailSSL ssl = new SendMailSSL();
					ssl.enviarMail(sender,password,receiver,content,title);
					
				}
			}
		});
		btnEnviar.setBounds(112, 162, 63, 23);
		getContentPane().add(btnEnviar);

		btngerarPDF = new JButton("Gerar Pdf");
		btngerarPDF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (e.getSource() == btngerarPDF) {
					String filename = JOptionPane
							.showInputDialog("Insira o nome do ficheiro");
					String content = "Voo " + textField.getText()
							+ " Cancelado\n" + "Data prevista para o Voo:"
							+ textField_4.getText() + "/"
							+ textField_5.getText() + "/"
							+ textField_6.getText();

					GeneratePdf pdf = new GeneratePdf();
					try {
						pdf.pdfConstructor(filename, content);
					} catch (DocumentException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		btngerarPDF.setBounds(185, 162, 89, 23);
		getContentPane().add(btngerarPDF);
	}
}
